package com.example.springboottestservice.annotations;

import com.example.springboottestservice.beans.Cat;
import com.example.springboottestservice.beans.Dog;
import com.example.springboottestservice.beans.User;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;

@Configuration
@Import({User.class,BeanSelector.class,BeanImportBeanDefinitionRegistrar.class})
public class BeansConfig {

    @Bean
    public User user(){
        return new User();
    }
}

class BeanSelector implements ImportSelector{

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{Cat.class.getName(), Dog.class.getName()};
    }
}

class BeanImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(User.class);
        registry.registerBeanDefinition("user2",rootBeanDefinition);
    }
}
